package cn.jdy.audit.service;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jdy.audit.entity.AdShow;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
/**
 * @author daihu
 * @title : 产品操作实现
 */
@Service
public class AdShowService extends BaseDao<AdShow> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * 
	 * @return
	 */
	public List<AdShow> query(AdShow adshow, String... date) {
		if (adshow == null)
			return super.findAll();
		if (adshow.getiDisplayLength() > 0) {
			return super.find(createFilter(adshow, date),
					adshow.getiDisplayStart(), adshow.getiDisplayLength());
		}
		return super.find(createFilter(adshow, date));
	}

	public Long count(AdShow adshow, String... date) {
		return super.count(createFilter(adshow, date));
	}

	/**
	 * 拼装条件
	 * 
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(AdShow adshow, String... date) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(adshow.getKeywords())) { //后台管理用到这个地方
      if(!StringUtil.isNullOrEmpty(adshow.getKeyType())) {
        dc.add(Restrictions.like(adshow.getKeyType(), adshow.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }	
		if (date.length > 1 && !StringUtil.isNullOrEmpty(date[0])
				&& !StringUtil.isNullOrEmpty(date[1])) {
			dc.add(Restrictions.between("add_time", DateUtil.parseDate(date[0]), DateUtil.parseDate(date[1])));
		}
		
		if (!StringUtil.isNullOrEmpty(adshow.getName())) {
			dc.add(Restrictions.like("name", adshow.getName(),MatchMode.ANYWHERE));
		}
		if (!StringUtil.isNullOrEmpty(adshow.getUrlsource())) {
			dc.add(Restrictions.like("urlsource", adshow.getUrlsource(),MatchMode.ANYWHERE));
		}
		dc.addOrder(Order.desc("createTime"));
		return dc;
	}

	/**
	 * 
	 * @param adshow
	 * @return
	 */
	public List<AdShow> querypage(AdShow adshow) {
		return super.find(createFilter(adshow),
				adshow.getPageNo(), 4);
	}

	/**
	 * 查询热门产品
	 * 
	 * @return
	 */
	public List<AdShow> queryHot() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.addOrder(Order.desc("readTimes"));
		return super.find(dc, 0, 5);

	}

	/**
	 * 按照类型查询 产品
	 * 
	 * @return
	 */
	public List<AdShow> findbytype(Long columnid, int firstResult,
			int maxResults, boolean isImg) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (null != columnid) {
			dc.add(Restrictions.eq("columnId", columnid));
		}
		if (isImg) {// 是否有封面图片
			dc.add(Restrictions.ne("imgUrl", ""));
		}
		dc.addOrder(Order.desc("createTime"));
		return super.find(dc, firstResult, maxResults);
	}

	/**
	 * 按照大分类 查询产品
	 * 
	 * @return
	 */
	public List<AdShow> findbyPid(Long pid, int firstResult, int maxResults,
			boolean isImg) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.createAlias("cv", "cv");
		if (isImg) {// 是否有封面图片
			dc.add(Restrictions.ne("imgUrl", ""));
		}
		if (null != pid) {
			dc.add(Restrictions.eq("cv.pid", pid));
		}
		dc.addOrder(Order.desc("createTime"));
		return super.find(dc, firstResult, maxResults);
	}

}
